package Sequence;

import java.util.Arrays;

public class Test0435 {
}


class Solution0435_1 {
    public int eraseOverlapIntervals(int[][] intervals) {
        Arrays.sort(intervals,(a,b)->{
            if (a[0]==b[0]) return b[1]-a[1];
            else return a[0]-b[0];
        });
        int ans=-1,lastIndex=Integer.MAX_VALUE;
        for (int[] interval : intervals) {
            if (interval[0]<lastIndex){
                lastIndex=Math.min(lastIndex,interval[1]);
                ans++;
            }else{
                lastIndex=interval[1];
            }
        }
        return ans;
    }
}